package com.yunfeng.supercall.activity;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.ly.quickdev.library.activity.DevBaseListActivity;
import com.ly.quickdev.library.adapter.ListBaseAdapter.ViewHolder;
import com.yunfeng.supercall.R;
import com.yunfeng.supercall.app.AppContext;
import com.yunfeng.supercall.bean.Constants;
import com.yunfeng.supercall.bean.LevelBean;
import com.yunfeng.supercall.bean.UserBean;
import com.yunfeng.supercall.httputils.HttpClient;
import com.yunfeng.supercall.httputils.MyAjaxCallBack;
import com.yunfeng.supercall.manager.ManagerFactory;

public class SuperSchoolActivity extends DevBaseListActivity<LevelBean> {

	@Override
	public void setContentView(int layoutResID) {
		// TODO Auto-generated method stub
		super.setContentView(R.layout.activity_simple_list);
		
		
		findViewById(R.id.imageview_back).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		findViewById(R.id.framelayout_back).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});

	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		loadNum();
	}

	private HttpClient mHttpClient = new HttpClient();

	@Override
	public void onSupportContentChanged() {
		// TODO Auto-generated method stub
		super.onSupportContentChanged();
		((TextView) findView(R.id.title)).setText(R.string.super_school);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		if (convertView == null) {
			convertView = getLayoutInflater().inflate(
					R.layout.list_item_super_school, null);
		}
		LevelBean level = mList.get(position);
		final TextView levleName = ViewHolder.get(convertView, R.id.level_name);
		final TextView des = ViewHolder.get(convertView, R.id.descri);
		AppContext.displayImgWithDefault1(
				(ImageView) ViewHolder.get(convertView, R.id.iv),
				level.getPhoto());
		levleName.setText(level.getLevelname());
		des.setText(level.getNote());
		return convertView;
	}

	View mView;

	@Override
	public void loadData() {
		// TODO Auto-generated method stub

		mView = getLayoutInflater().inflate(R.layout.list_header_super_school,
				null);
		refresh();
		mListView.getRefreshableView().addHeaderView(mView, null, false);
		setListAdapter();
	}

	private int mShareNum, mFansNum;

	private void loadNum() {
		JSONObject json = BaseActivity.getBaseParams(getActivity());
		mHttpClient.post(Constants.FUNCTION_GET_USER_FREINDS_NUM, json,
				new MyAjaxCallBack() {

					@Override
					public void onReceiveError(String msg, String errorCode) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onReceiveData(String data, String msg) {
						// TODO Auto-generated method stub
						try {
							JSONObject json = new JSONObject(data.substring(1,
									data.length() - 1));
							mShareNum = json.getInt("sharenum");
							mFansNum = json.getInt("fensinum");
							refresh();
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				});
	}

	private void refresh() {
		List<LevelBean> levels = ManagerFactory.createManager(LevelBean.class)
				.getList();
		if (mList == null) {
			mList = new ArrayList<LevelBean>();
		}
		mList.clear();
		UserBean user = BaseActivity.getUser(getActivity());
		String name = "";
		for (LevelBean l : levels) {
			if (l.getLevelid() <= user.getLevel()) {
				mList.add(l);
			}
			if (l.getLevelid() == user.getLevel()) {
				name = l.getLevelname();
			}
		}
		TextView tv = (TextView) mView.findViewById(R.id.text);
		// tv.setText(ContactsDialPadAdapter.replaceTextColor("恭喜您升级到" + name
		// + "以上阶段<br/>现在去填写入园申请，加入我们吧", "入园申请"));
		// tv.setText("恭喜你升级到" + name + "阶段\n成功分享:" + mShareNum + "   粉丝:"
		// + mFansNum);
		tv.setText(getString(R.string.has_been_level, name, mShareNum, mFansNum));
	}

	@Override
	public Mode getRefreshMode() {
		// TODO Auto-generated method stub
		return Mode.DISABLED;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO Auto-generated method stub

	}
}
